package yja.gzw.app.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.kongzue.dialogx.dialogs.CustomDialog
import com.kongzue.dialogx.dialogs.WaitDialog.dismiss
import com.kongzue.dialogx.interfaces.OnBindView
import yja.gzw.app.R
import yja.gzw.app.activity.BianjiJingpingshipingfabuActivity
import yja.gzw.app.activity.BianjizhuanquActivity
import yja.gzw.app.activity.FabidingdanActivity
import yja.gzw.app.activity.JingspxiangqingActivity
import yja.gzw.app.activity.WodefabuActivity
import yja.gzw.app.activity.ZhquxActivity
import yja.gzw.app.model.UserData
import yja.gzw.app.model.bean.Order
import yja.gzw.app.viewmodel.Mainviewmodel

class FabuddAdapter (private val content: Context, private val list: MutableList<Order>): RecyclerView.Adapter<RecyclerView.ViewHolder>(){
    val viewmodel= Mainviewmodel()
    private var view: View?=null
    private lateinit var bottomSheetDialog: BottomSheetDialog
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        view= LayoutInflater.from(parent.context).inflate(R.layout.item_view_fabudd,parent,false)
        return ViewHodeView(view!!)
    }

    override fun getItemCount(): Int {
            return list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        if(holder is ViewHodeView){

            holder.shanchu.visibility=View.GONE

            holder.biaotis.text=list[position].title
            holder.cishu.text=list[position].release_num.toString()
            holder.pingtai.text=list[position].platform_id
            holder.shipinbiaoti.text=list[position].content

            if(list[position].type=="1"){
                holder.type.text="3元专区"
            }else if(list[position].type=="2"){
                holder.type.text="4元专区"
            }else {
                    holder.show1.visibility=View.GONE
                holder.show2.visibility=View.GONE
                holder.show3.visibility=View.GONE
                holder.leixingss.visibility=View.VISIBLE


                if(list[position].fine_release_type==1){
                    holder.leixingss.text="已出价￥${list[position].pay_price}"
                    holder.zengjiachujia.visibility=View.VISIBLE
                }else if(list[position].fine_release_type==2){
                    holder.leixingss.text="待第二方出价"
                    holder.zengjiachujia.visibility=View.GONE
                }else{
                    holder.leixingss.text="第二方竞价中"
                    holder.zengjiachujia.visibility=View.GONE
                }
                holder.type.text="精品视频"

            }



             holder.bianji.setOnClickListener {
                if(list[position].type=="1"|| list[position].type=="2"){
                    val inttnt= Intent(content, BianjizhuanquActivity::class.java)
                    inttnt.putExtra("id",list[position].id.toString())
                    content.startActivity(inttnt)

                }else{
                //todo 添加编辑精品视屏
                    val inttnt= Intent(content, BianjiJingpingshipingfabuActivity::class.java)
                    inttnt.putExtra("id",list[position].id.toString())
                    content.startActivity(inttnt)
                }


             }
            holder.itemView.setOnClickListener {
                if(list[position].type=="1"|| list[position].type=="2"){

                    val intent = Intent(content, ZhquxActivity::class.java)

                    intent.putExtra("id",list[position].id.toString())
                    intent.putExtra("youid",list[position].user_id.toString())
                    content.startActivity(intent)
                }else{

                    val intent=Intent(content, JingspxiangqingActivity::class.java)
                    intent.putExtra("id", list[position].id.toString())
                    content.startActivity(intent)



                }


            }

            holder.shanchu.setOnClickListener {


                CustomDialog.show(object : OnBindView<CustomDialog>(R.layout.layout_custom_tishi) {
                    override fun onBind(dialog: CustomDialog, v: View) {
                        val tongyi = v.findViewById<TextView>(R.id.tongyi)
                        val butongyi = v.findViewById<TextView>(R.id.butongyi)

                        tongyi.setOnClickListener {
                            viewmodel.onshanchudingdanwang(UserData.token,mapOf("id" to list[position].id.toString()))
                            viewmodel.onshanchudingdan.observe(content as WodefabuActivity, Observer {
                                if(it.code==1){
                                    list.removeAt(position)
                                    dialog.dismiss()
                                    // 通知适配器数据集已更改
                                    notifyItemRemoved(position)
                                    notifyItemRangeChanged(position, list.size - position)

                                }else{
                                    Toast.makeText(content,it.msg, Toast.LENGTH_SHORT).show()

                                }

                            })

                        }

                        butongyi.setOnClickListener {
                            dialog.dismiss()
                        }



                    }
                })
                    .setMaskColor(content.getColor(R.color.touming))



            }

            holder.zengjiachujia.setOnClickListener {

                bottomSheetDialog = BottomSheetDialog(content)
                val view = LayoutInflater.from(content).inflate(R.layout.bottom_sheet_chujia, null)
                bottomSheetDialog.setContentView(view)
                val chacah=view.findViewById<ImageView>(R.id.chacha)
                val queding=view.findViewById<TextView>(R.id.queding)
                val chujiarushukuang=view.findViewById<EditText>(R.id.chujiarushukuang)
                chacah.setOnClickListener {
                    bottomSheetDialog.dismiss()
                }

                queding.setOnClickListener {


                    val zhi = chujiarushukuang.text.toString().toIntOrNull()

                    if(zhi!!<=40){
                        Toast.makeText(content, "价格不能低于40", Toast.LENGTH_SHORT).show()
                    }else{

                        viewmodel.onbianjifabujingpingshipingwang(UserData.token,mapOf("title" to list[position].title,"fine_release_type" to list[position].fine_release_type.toString(),"endtime" to "","remark" to list[position].remark,"material" to list[position].material as String,"pay_price" to zhi.toString(),"order_id" to list[position].id.toString()))

                        viewmodel.onbianjifabujingpingshiping.observe(content as WodefabuActivity, Observer {
                            if(it.code==1){

                                bottomSheetDialog.dismiss()
                            }else{
                                Toast.makeText(content,it.msg, Toast.LENGTH_SHORT).show()

                            }


                        })
                    }

                }

                bottomSheetDialog.show()
            }









        }


    }

    inner class ViewHodeView(itemView: View):RecyclerView.ViewHolder(itemView){

        val biaotis: TextView =itemView.findViewById(R.id.zhuanquname)
        val cishu: TextView =itemView.findViewById(R.id.cishu)
        val pingtai: TextView =itemView.findViewById(R.id.fabupingtai)
        val shipinbiaoti: TextView =itemView.findViewById(R.id.biaoti)
        val type: TextView =itemView.findViewById(R.id.leixing)
        val zengjiachujia:TextView=itemView.findViewById(R.id.zengjiachujia)
        val bianji:TextView=itemView.findViewById(R.id.bianji)
        val shanchu:TextView=itemView.findViewById(R.id.shanchu)
        val show1: LinearLayout =itemView.findViewById(R.id.show1)
        val show2: LinearLayout =itemView.findViewById(R.id.show2)
        val show3: LinearLayout =itemView.findViewById(R.id.show3)
        val leixingss: TextView =itemView.findViewById(R.id.leixingss)



    }


}